package com.android.pagemarket.ui.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.pagemarket.R;
import com.android.pagemarket.ui.activity.base.TemplateActivity;
import com.android.pagemarket.ui.widget.TitleBar;

public class VideoShowActivity extends TemplateActivity 
implements OnClickListener,OnErrorListener,OnCompletionListener,OnPreparedListener
{
    private VideoView mVideoView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_video_show);
        initViews();        
    }
    
    
    @Override
    protected void setupTitleBar(TitleBar titleBar) 
    {
        titleBar.setVisibility(View.GONE);
    }
    
    @Override
    public void onClick(View view) 
    {
        finish();
    }
    
    @Override
    protected void onResume() 
    {
        super.onResume();
        getWindow().getDecorView().setKeepScreenOn(true);
        if(null != mVideoView)
        {
            mVideoView.resume();
        }
    }
    
    @Override
    protected void onPause() 
    {
        super.onPause();
        getWindow().getDecorView().setKeepScreenOn(false);
        if(null != mVideoView) 
        {
            mVideoView.pause();
        }
    }
    
    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        if(null != mVideoView)
        {
            mVideoView.stopPlayback();
            mVideoView = null;
        }
    }
    

    @Override
    public void onPrepared(MediaPlayer arg0)
    {
        showLoading(false);
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) 
    {
        if(what == MediaPlayer.MEDIA_ERROR_UNKNOWN)
        {
            
        } else if(what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) 
        {
            
        }
        Toast.makeText(this, "报错了", Toast.LENGTH_LONG).show();
        finish();
        return true;
    }

    @Override
    public void onCompletion(MediaPlayer mp) 
    { 
        System.out.println("onCompletion");
        if(null != mVideoView) 
        {
            mVideoView.seekTo(0);
            mVideoView.stopPlayback();
        }
        finish();
    }
    
    private void initViews()
    {
        Button pass_video = (Button)findViewById(R.id.pass_video); 
        mVideoView   =(VideoView)findViewById(R.id.video);
        
        mVideoView.setOnErrorListener(this);
        mVideoView.setOnCompletionListener(this);
        mVideoView.setOnPreparedListener(this);
        //播放控制器
        MediaController mMediaCtrl = new MediaController(this);
        mMediaCtrl.setAnchorView(mVideoView);
        mVideoView.setMediaController(mMediaCtrl);
        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/raw/welcome"));
        mVideoView.requestFocus();
        showLoading(true);
        mVideoView.start();
        pass_video.setOnClickListener(this);
    }
    
    public static void startActivity(Context context)
    {
        Intent it = new Intent(context, VideoShowActivity.class);
        
        context.startActivity(it);
    }
}